import { Component,ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams,Content } from 'ionic-angular';
import { ConfigProvider } from '../../providers/config/config';
import { HttpServicesProvider } from '../../providers/http-services/http-services';

/**
 * Generated class for the SearchPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-search',
  templateUrl: 'search.html',
})
export class SearchPage {
  // 装饰器 this.content.scrollToTop();回到顶部
  @ViewChild(Content) content:Content;
  public flag = false //有没有关键词进行控制
  public list: Array<any> = []//列表页

  public keywords = ''//关键字

  public page: number = 1//分页

  public  hasData = true //是否有数据

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public config: ConfigProvider,
    public httpServices: HttpServicesProvider
  ) {
  }

  // 获取搜索列表
  getSearchList(ionInfinite) {

    if(!ionInfinite){
      this.page = 1
      this.hasData = true
      this.content.scrollToTop(0)//回到顶部
    }
    this.httpServices.requestData('api/plist?search=' + this.keywords + '&page=' + this.page, data => {
      if(this.page == 1){//第一页替换数据
        this.list = data.result
      }else{
        // 第二页拼接数据
        this.list = this.list.concat(data.result)
      }
      
      this.flag = true
      if (ionInfinite) {
        ionInfinite.complete()
        // 如果没有数据停止上拉更新
        if(data.result <10){
          this.hasData = false
        }
      }
      this.page++
    })
  }

  doLoadMore(ionInfinite) {
    this.getSearchList(ionInfinite)
  }

}
